import type { SearchFormSchema } from '@/components/searchform';
import { Selector } from 'gantd';
import request from '@/utils/request';
import { getRecipientListWithName, bomRoomMerge } from './util';

export const tableSchema: any = [
  {
    fieldName: 'productLine',
    title: '产品线',
    width: 180,
    valueFormatter: (params: any) => {
      return params.value;
    },
  },
  {
    fieldName: 'bomRoom',
    title: 'MBOM房间',
    width: 180,
    editConfig: {
      component: Selector,
      props: {
        selectorId: 'RecipientList_table_bomRoom',
        // 表格刷新再state更新后加载
        query: async function (word) {
          const baseSearch = {
            bomType: 'MBOM',
          };
          if (word) {
            baseSearch.batchNo = word;
          }
          const ls = await request.post('/bommgmt/receivedVehicle/findByCriteria', {
            data: {
              filterInfo: baseSearch,
              pageInfo: { beginIndex: 0, pageSize: 1500 },
            },
          });
          const finallLs = ls.filter((item) => +item?.level === 0);
          return bomRoomMerge(finallLs || []);
        },
        labelProp: 'batchNo', // 下拉项得展示
        valueProp: 'batchNo',
        useStorage: true,
        useCache: true,
        allowClear: true,
      },
      rules: {
        required: true,
        whitespace: true,
        message: 'BOM房间必选',
      },
      editable: true,
    },
  },
  {
    fieldName: 'recipientList',
    title: '收件人列表',
    flex: 1,
    valueFormatter: (params: any) => {
      return getRecipientListWithName(params?.value);
    },
  },
];

export const searchSchema: SearchFormSchema = {
  productLine: {
    title: '产品线',
  },
  bomRoom: {
    title: 'MBOM房间',
    componentType: 'Selector',
    props: {
      selectorId: 'RecipientList_search_bomRoom',
      query: async function (word) {
        const baseSearch = {
          bomType: 'MBOM',
        };
        if (word) {
          baseSearch.batchNo = word;
        }
        const ls = await request.post('/bommgmt/receivedVehicle/findByCriteria', {
          data: {
            filterInfo: baseSearch,
            pageInfo: { beginIndex: 0, pageSize: 1500 },
          },
        });
        const finallLs = ls.filter((item) => +item?.level === 0);
        return bomRoomMerge(finallLs || []);
      },
      labelProp: 'batchNo', // 下拉项得展示
      valueProp: 'batchNo',
      useStorage: true,
      useCache: true,
      allowClear: true,
    },
  },
};
